<?php

namespace app\common\model\product;
use app\common\model\file\FileModel;
use app\common\model\setting\SettingStorePlatformModel;
use think\Model;

class ShopProductTypeModel extends Model
{
    // 表名
    protected $name = 'shop_product_type';
    // 表主键
    protected $pk = 'id';
    const PTBT = 'PTBT';//平台补贴
    const DFRW = 'DFRW';//垫付任务
    const LLRW = 'LLRW';//浏览任务
    // 关联图片
    public function image()
    {
        return $this->hasOne(FileModel::class, 'file_id', 'image_id')->append(['file_url'])->where(where_disdel());
    }
    /**
     * 获取图片链接
     * @Apidoc\Field("")
     * @Apidoc\AddField("image_url", type="string", desc="图片链接")
     */
    public function getImageUrlAttr($value, $data)
    {
        return $this['image']['file_url'] ?? '';
    }

    // 关联平台
    public function platforms()
    {
        return $this->belongsToMany(SettingStorePlatformModel::class, ShopProductTypeAttributesModel::class, 'setting_store_platform_id', 'shop_product_type_id');
    }
    /**
     * 获取平台id
     * @Apidoc\Field("")
     * @Apidoc\AddField("platform_ids", type="array", desc="平台id")
     */
    public function getPlatformIdsAttr()
    {
        return relation_fields($this['platforms'], 'id');
    }
    /**
     * 获取平台名称
     * @Apidoc\Field("")
     * @Apidoc\AddField("platform_names", type="string", desc="平台名称")
     */
    public function getPlatformNamesAttr()
    {
        return relation_fields($this['platforms'], 'title', true);
    }
}
